package com.css.zfzx.fwsjcj.modules.apkDown;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

@RequestMapping("/apk")
@RestController
public class ApkDownController {
    @GetMapping("downApk")
    public void downApk(HttpServletResponse response) throws FileNotFoundException {
        // 下载本地文件
        String fileName = "fwsjcj.apk".toString(); // 文件的默认保存名
        // 读到流中
        InputStream inStream = new FileInputStream("D:/androidApk/fwsjcj.apk");// 文件的存放路径
        // 设置输出的格式
        response.reset();
        response.setContentType("bin");
        response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        // 循环取出流中的数据
        byte[] b = new byte[100];
        int len;
        try {
            while ((len = inStream.read(b)) > 0)
                response.getOutputStream().write(b, 0, len);
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
